/*
 * Created on Aug 21, 2010 by Eng. Julian Rousselot
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package ar.com.visualAORE.util;

import java.awt.Color;
import java.io.File;
import java.util.HashSet;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import ar.com.visualAORE.model.Concern;
 
public class ReadConcernsFromXMLFile {
 
public static Set<Concern> getPersistedConcernSet(){ 
    Set<Concern> concernSet = new HashSet<Concern>();    
    try {
 
    File fXmlFile = new File("src/concerns-list.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
 
    doc.getDocumentElement().normalize();
 
    NodeList nList = doc.getElementsByTagName("concern");
 
    for (int temp = 0; temp < nList.getLength(); temp++) {
 
        Node nNode = nList.item(temp);
 
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 
            Element eElement = (Element) nNode;
            Color color = new Color(Integer.parseInt(eElement.getElementsByTagName("color").item(0).getTextContent()));
            Concern concern = new Concern(eElement.getElementsByTagName("name").item(0).getTextContent(), 
                                                    null, color);
            concernSet.add(concern);
        }
    }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return concernSet;
}
 
}